package com.yuanluludev.imageloader.glide

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.widget.ImageView
import androidx.annotation.DrawableRes
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.yuanluludev.imageloader.R
import com.yuanluludev.lib_utils.util.SizeUtils

/**
 *    yuanLuLu
 *    time: 2019/12/15
 */
object GlideManager {

    /**
     * 占位图
     */
    var RES_ID_PLACE_HOLDER: Int = R.drawable.image_loading_ic

    /**
     * 错误图
     */
    var RES_ID_ERROR: Int = R.drawable.image_error_ic

    /**
     * 从远程地址中加载图片
     */
    @SuppressLint("CheckResult")
    fun load(context: Context, url: String, iv: ImageView, placeholder: Int = -1, error: Int = -1) {
        GlideApp.with(context)
            .load(url)
            .placeholder(if (placeholder == -1) RES_ID_PLACE_HOLDER else placeholder)
            .error(if (error == -1) RES_ID_ERROR else error)
            .into(iv)
    }

    /**
     * 从drawable中加载图片
     */
    @SuppressLint("CheckResult")
    fun load(
        context: Context,
        @DrawableRes resourceId: Int,
        iv: ImageView,
        placeholder: Int = -1,
        error: Int = -1
    ) {
        GlideApp.with(context)
            .load(resourceId)
            .placeholder(if (placeholder == -1) RES_ID_PLACE_HOLDER else placeholder)
            .error(if (error == -1) RES_ID_ERROR else error)
            .into(iv)
    }

    /**
     * 从远程地址中加载圆形的图片
     */
    @SuppressLint("CheckResult")
    fun loadCircle(
        context: Context,
        url: String,
        iv: ImageView,
        placeholder: Int = -1,
        error: Int = -1
    ) {
        GlideApp.with(context)
            .load(url)
            .placeholder(if (placeholder == -1) RES_ID_PLACE_HOLDER else placeholder)
            .error(if (error == -1) RES_ID_ERROR else error)
            .transform(MultiTransformation(CenterCrop(), CircleCrop()))
            .into(iv)
    }

    /**
     * 从drawable中加载圆形的图片
     */
    @SuppressLint("CheckResult")
    fun loadCircle(
        context: Context,
        @DrawableRes resourceId: Int,
        iv: ImageView,
        placeholder: Int = -1,
        error: Int = -1
    ) {
        GlideApp.with(context)
            .load(resourceId)
            .placeholder(if (placeholder == -1) RES_ID_PLACE_HOLDER else placeholder)
            .error(if (error == -1) RES_ID_ERROR else error)
            .transform(MultiTransformation(CenterCrop(), CircleCrop()))
            .into(iv)
    }


    /**
     * 从远程地址中加载圆角的图片
     */
    @SuppressLint("CheckResult")
    fun loadRound(
        context: Context,
        url: String,
        dpValue: Int,
        iv: ImageView,
        placeholder: Int = -1,
        error: Int = -1
    ) {
        GlideApp.with(context)
            .load(url)
            .placeholder(if (placeholder == -1) RES_ID_PLACE_HOLDER else placeholder)
            .error(if (error == -1) RES_ID_ERROR else error)
            .transform(
                MultiTransformation(
                    CenterCrop(),
                    RoundedCorners(SizeUtils.dp2px(dpValue.toFloat()))
                )
            )
            .into(iv)
    }

    /**
     * 从drawable中加载圆角的图片
     */
    @SuppressLint("CheckResult")
    fun loadRound(
        context: Context,
        @DrawableRes resourceId: Int,
        dpValue: Int,
        iv: ImageView,
        placeholder: Int = -1,
        error: Int = -1
    ) {
        GlideApp.with(context)
            .load(resourceId)
            .placeholder(if (placeholder == -1) RES_ID_PLACE_HOLDER else placeholder)
            .error(if (error == -1) RES_ID_ERROR else error)
            .transform(
                MultiTransformation(
                    CenterCrop(),
                    RoundedCorners(SizeUtils.dp2px(dpValue.toFloat()))
                )
            )
            .into(iv)
    }

    /**
     * 清理所有图片内存缓存(在application中使用)
     */
    fun onLowMemory(app: Application) {
        GlideApp.get(app).onLowMemory()
    }

    /**
     * 根据手机内存剩余情况清理图片内存缓存（在application中使用）
     */
    fun onTrimMemory(app: Application, level: Int) {
        GlideApp.get(app).onTrimMemory(level)
    }

    /**
     *  清除内存缓存（必须在主线程）
     */
    fun clearMemory(context: Context) {
        GlideApp.get(context).clearMemory()
    }

    /**
     * 清除本地缓存（必须在子线程）
     */
    fun clearDiskCache(context: Context) {
        GlideApp.get(context).clearDiskCache()
    }

}


